﻿using RabbitMQ.Client.Events;
using RabbitMQ.Client;
using System.Text;
using EasyNetQ;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Csb.RabbitMQ.Common.Components.RabbitMq.EasyNetQ;

namespace Csb.RabbitMQ.ServerApp
{
    internal class Program
    {
        static async Task Main(string[] args)
        {
            Console.WriteLine("RabbitMQ 接收消息服务端启动...");

            try
            {
                await CreateHostBuilder(args).Build().RunAsync();
            }
            catch (Exception ex)
            {
                Console.WriteLine("An error occurred: " + ex.Message);
            }


        }


        public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .ConfigureServices((hostContext, services) =>
            {
                // 配置 EasyNetQ 使用 Newtonsoft.Json 序列化器
                //services.AddSingleton<ISerializer, JsonSerializerAdapter>();

                //// 配置 EasyNetQ 连接字符串
                services.AddSingleton<IBus>(sp =>
                {
                    var bus = RabbitHutch.CreateBus("host=127.0.0.1;virtualHost=/;username=guest;password=guest");
                    return bus;
                });

                //services.RegisterEasyNetQ("host=127.0.0.1;virtualHost=/;username=guest;password=guest");

                // 注册 MQSubscribe
                services.AddSingleton<MQSubscribe>();
                // 注册 MQPublish 
                services.AddSingleton<MQPublish>();

                // 注册 SubscribeWorker 作为后台服务
                services.AddHostedService<SubscribeWorker>();
            });


    }
}
